function page(start,count) {
    document.formBody.gotoPage.value = '';
    document.formBody.start.value = start;
    document.formBody.count.value = count;
    document.formBody.submit();
//    document.formBody.appendChild(document.createElement("button")).click();
}


function countValue(amount,price){
    return document.formBody.mustbuy=amount*price;
}

function gotoPager() {
    if (document.formBody.gotoPage.value >= 1) {
        if (document.formBody.count.value == '' || document.formBody.count.value == 0) document.formBody.count.value = 10;
        var a = (document.formBody.gotoPage.value - 1) * document.formBody.count.value;
        document.formBody.start.value = a;
        document.formBody.gotoPage2.value = document.formBody.gotoPage.value;
        document.formBody.submit();
    //        document.formBody.appendChild(document.createElement("button")).click();
    }
}

function gotoPager2() {
    if (document.formBody.gotoPage2.value >= 1) {
        if (document.formBody.count.value == '' || document.formBody.count.value == 0) document.formBody.count.value = 10;
        var a = (document.formBody.gotoPage2.value - 1) * document.formBody.count.value;
        document.formBody.start.value = a;
        document.formBody.gotoPage.value = document.formBody.gotoPage2.value;
        document.formBody.submit();
    //        document.formBody.appendChild(document.createElement("button")).click();
    }
}

function sort(orderBy) {
    document.formBody.orderBy.value = orderBy;
    if (document.formBody.ascDesc.value=='') {
        document.formBody.ascDesc.value='desc';
    } else if (document.formBody.ascDesc.value=='desc') {
        document.formBody.ascDesc.value='asc';
    } else {
        document.formBody.ascDesc.value='desc';
    }
    document.formBody.submit();
//    document.formBody.appendChild(document.createElement("button")).click();
}

function selectAllOptions(id) {
    var list = document.getElementById(id);
    for (var i = 0; i < list.options.length; i++){
        list.options[i].selected = true;
    }
    return true;
}

function sortForTrxReport(orderBy, id) {
    document.formBody.orderBy.value = orderBy;
    document.formBody.orderId.value = id;
    if (document.formBody.ascDesc.value=='') {
        document.formBody.ascDesc.value='desc';
    } else if (document.formBody.ascDesc.value=='desc') {
        document.formBody.ascDesc.value='asc';
    } else {
        document.formBody.ascDesc.value='desc';
    }
    document.formBody.submit();
//    document.formBody.appendChild(document.createElement("button")).click();
}

function showModalDialog(title, ajax, content, url) {
    var head = "<div class='customModal' class='modal hide fade modal-large' style='display: none;'><div class='modal-header'>";
    head = head + "<button class='close' data-dismiss='modal'>&times;</button>";
    head = head + "<h3>" + title + "</h3></div>"
    head = head + "<div class='modal-body'><div class='customModalContent'></div></div>";
    head = head + "<div class='modal-footer'>";
    head = head + "<a href='#' class='btn' data-dismiss='modal' id='abmCancelBtn'>Close</a>";
    head = head + "</div></div>";
}

!function ($) {
    $(function(){
        // fix sub nav on scroll
        var $win = $(window)
        , $nav = $('.subnav')
        , navTop = $('.navbar').length && $('.navbar').offset().top + 246
        , isFixed = 0
        , $boContentWrapper = $('#boContentWrapper')
        , $boSideNavWrapper = $('#boSideNavWrapper')

        processScroll()

        // hack sad times - holdover until rewrite for 2.1
        $nav.on('click', function () {
            if (!isFixed) setTimeout(function () {
                $win.scrollTop($win.scrollTop() - 11)
            }, 10)
        })

        $win.on('scroll', processScroll)
        $win.on('resize', processResize)

        function processResize() {
            var winWidth = document.body.offsetWidth
            if(winWidth <= 986) {
                $boSideNavWrapper.css('display', 'none')
                $boSideNavWrapper.removeClass('span3')
                $nav.css('display', '')
            }
        }

        function processScroll() {
            var i, scrollTop = $win.scrollTop(), winWidth = document.body.offsetWidth
            //        console.log('navTop    :' + navTop)
            //        console.log('scrollTop :' + scrollTop)
            if(winWidth <= 986) {
                $boSideNavWrapper.css('display', 'none')
                $boSideNavWrapper.removeClass('span3')
                $nav.css('display', '')
            }
            if (scrollTop >= navTop && !isFixed && winWidth > 768) {
                isFixed = 1
                $nav.css('display', '')
                $boSideNavWrapper.css('display', 'none')
                $boSideNavWrapper.removeClass('span3')
                $boContentWrapper.addClass('addTopContent',10)
                $nav.addClass('subnav-fixed',50)
                $boContentWrapper.removeClass('span9',100)
            } else if (scrollTop <= navTop && isFixed) {
                isFixed = 0
                $nav.css('display', 'none')
                $nav.removeClass('subnav-fixed',50)
                $boContentWrapper.removeClass('addTopContent',10)
                $boContentWrapper.addClass('span9',100)
                if(winWidth > 986) {
                    $boSideNavWrapper.css('display','block')
                    $boSideNavWrapper.addClass('span3',50)
                }
            }
        }
    })
}(window.jQuery)

function createAlertDiv(type, title, content) {
    var alertMsg = '';
    var typeMsg = '';
    if(type == 'error') {
        typeMsg = 'alert-error';
    } else if(type == 'success') {
        typeMsg = 'alert-success';
    }
    if(content) {} else {
        content = '';
    }
    var head = "<div class='alert alert-block " + typeMsg + " fade in'>" +
    "<button class='close' data-dismiss='alert'>&times;</button>" +
    "<h4 class='alert-heading'>" + title + "</h4>";
    var tail = "</div>";
    alertMsg = head + content + tail;
    return alertMsg;
}

(function( $ ){
    $.fn.serializeJSON=function() {
        var json = {};
        jQuery.map($(this).serializeArray(), function(n, i){
            json[n['name']] = n['value'];
        });
        return json;
    };
})( window.jQuery );
